
package com.iot.common.core.util;

import com.iot.common.core.constant.CommonConstants;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	private int code;

	@Getter
	@Setter
	private String message;

	@Getter
	@Setter
	private T data;

	public static <T> R<T> ok() {
		return restResult(CommonConstants.SUCCESS, " 成功.", null);
	}

	public static <T> R<T> ok(T data) {
		return restResult(CommonConstants.SUCCESS, " 成功.", data);
	}

	public static <T> R<T> ok(String msg, T data) {
		return restResult(CommonConstants.SUCCESS, msg, data);
	}

	public static <T> R<T> failed() {
		return restResult(CommonConstants.FAIL, "失败.", null);
	}

	
	public static <T> R<T> failed(String msg) {
		return restResult(CommonConstants.FAIL, msg, null);
	}

	public static <T> R<T> failed(T data) {
		return restResult(CommonConstants.FAIL, "失败.", data);
	}

	public static <T> R<T> failed(String msg, T data) {
		return restResult(CommonConstants.FAIL, msg, data);
	}
	
	public static <T> R<T> failed(int code, String msg) {
		return restResult(code, msg, null);
	}
	
	public static <T> R<T> failed(int code, String msg,T data) {
		return restResult(code, msg, data);
	}
	
	
	public static <T> R<T> warn() {
		return restResult(CommonConstants.WARN, "警告.", null);
	}

	
	public static <T> R<T> warn(String msg) {
		return restResult(CommonConstants.WARN, msg, null);
	}

	public static <T> R<T> warn(T data) {
		return restResult(CommonConstants.WARN, "警告.", data);
	}

	public static <T> R<T> warn(String msg, T data) {
		return restResult(CommonConstants.WARN, msg, data);
	}
	
	public static <T> R<T> restResult(int code, String msg, T data) {
		R<T> apiResult = new R<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMessage(msg);
		return apiResult;
	}

}
